<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/4/12
 * Time: 10:30
 */

namespace app\index\controller;

use think\template;
use think\Controller;
use app\index\controller\BootstrapZs7y;
use Gregwar\Captcha\CaptchaBuilder;

class ArticleDetail extends GetSlides
{
    protected $C;
    public function __construct()
    {
        parent::__construct();
        $this->C = new ApiCaptcha();
    }

    public function cdx()
    {
        $this->redirect('/');   //重定向

    }

    public function _empty()
    {
        $this->redirect('/');   //空操作 解决用户恶意的输入 报错影响交互
    }

    public function page_detail()
    {
        $this->get_slider();//返回banner
        $A = new Api();
        $external_url = $A->get_p_detail(input('get.id'));
        if ($external_url['data'][0]['attributes']['external_url'] != null) {
            $url = $external_url['data'][0]['attributes']['external_url'];
            $this->redirect($url);
        } else {
            $data = $A->get_p_detail(input('get.id'));
            $data = getImg($data);
            $this->assign("data", $data['data']);
            $this->assign("web_title", "文章详情");
            return view();
        }
    }

    public function page_more()
    {//浏览所有文章
        $this->get_slider();//返回banner
        if (COLUMNS[input('get.column')] == 'dtjs') {
            return $this->redirect('PartyBuilding/party_build');
        }
        if (COLUMNS[input('get.column')] == 'ky') {
            return $this->redirect('Research/scientific_research');
        }
        if (COLUMNS[input('get.column')] == 'jx') {
            return $this->redirect('Teach/education');
        }
        if (COLUMNS[input('get.column')] == 'rczp') {
            return $this->redirect('Recruitment/recruitment');
        }
        $A = new Api();
        $get_column = $A->get_menu('columns'); //获取栏目信息
        //By xxt 二级栏目 文章内容转 第一子选项（规章制度）------------------------------------
        $dar3 = get_child_columns(input('get.column'), $get_column['data']);
        $GLOBALS['has_dar3'] = false;
        if (!empty($dar3)) {
            return $this->redirect('ArticleDetail/page_more',['column' => $dar3[0]['id']]);
        }
        //--------------------------------------------------------------------------------
        if (COLUMNS[input('get.column')] == '领导风采') {
            $leader = $A->get_d_d_detail(DEPARTMENT['领导库']);
            $this->assign("leader", $leader['data']);
        }
        if (input('get.platform')) {
            $data = $A->get_platform_article(input('get.platform'), COLUMNS[input('get.column')],0,UNLIMITED_NUM);
        } else {
            $data = $A->get_sub_articlelist(1, input('get.column'), UNLIMITED_NUM, 0);
        }
        $data = getImg($data);
        $page = input('get.page');//分页
        $listRow = 5;
        if (isset($page) && null !== $page && $page * $listRow <= count($data['data'])) {
            $currentPage = $page; // 当前页
        } else if ($page * $listRow > count($data['data'])) {
            $currentPage = ceil(count($data['data']) / $listRow);
        } else {
            $currentPage = 1;
        }
        $bootstrap = BootstrapZs7y::make($data, $listRow, $currentPage, count($data['data']), false, ['path' => url('ArticleDetail/page_more'), 'query' => ['column' => input('get.column'), 'platform' => input('get.platform')]]);
        foreach ($data['data'] as $key => $value) {  //添加图片地址
            if ($data['data'][$key]['attributes']['external_url'] != null) {
                $data['data'][$key]['external_url'] = '_blank';
            } else {
                $data['data'][$key]['external_url'] = '';
            }
        }
        $data['data'] = array_slice($data['data'], ($currentPage - 1) * $bootstrap->listRows(), $bootstrap->listRows());



        //By xxt 三级栏目替换成原二级栏目--------------------------------------------------------------
            //获取栏目信息
        $des_col = [];
        foreach ($get_column['data'] as $column) {
            if ($column['id']==input('get.column')) $des_col = $column;
        }
            //如果是三级栏目则替换二级栏目，获取后面栏目信息
        $menu_col = input('get.column');
        if (get_level($des_col,$get_column['data'])==3) {
            $p_id = $des_col['relationships']['parent']['data'][0]['id'];
            $menu_col = $p_id;
            $GLOBALS['has_dar3'] = true;
            $dar3 = get_child_columns($p_id, $get_column['data']);
        } else $dar3 = get_child_columns(input('get.column'),$get_column['data']);
        //一级栏目确定，显示高亮
        $GLOBALS['dar1'] = COLUMNS[find_the_dar1($A, $get_column, input('get.column'))];
        //----------------------------------------------------------------------------------------

        $column = [];//当前栏目
        $dar1 = [];//一级栏目
        $dar2 = [];//二级栏目
        $k = 0;
        // 获取所有栏目
        for ($i = 0; $i < count($get_column['data']); $i++) {
            // 获取当前页面的栏目信息
            if ($get_column['data'][$i]['id'] == $menu_col) {
                // 如果该栏目有父级栏目信息
                if ($get_column['data'][$i]['relationships']['parent']['data']) {
                    //获取父级栏目id
                    $parentId = $get_column['data'][$i]['relationships']['parent']['data'][0]['id'];
                    //遍历全部栏目
                    for ($j = 0; $j < count($get_column['data']); $j++) {
                        //找到父级栏目
                        if ($get_column['data'][$j]['id'] == $parentId) {
                            $dar1['name'] = $get_column['data'][$j]['attributes']['name'];
                        }
                        // 如果栏目有父级栏目
                        if ($get_column['data'][$j]['relationships']['parent']['data']) {
                            // 如果该栏目的父级栏目和当前页面父级栏目相同
                            if ($get_column['data'][$j]['relationships']['parent']['data'][0]['id'] == $parentId) {

                                $key = ($k + 1);
                                $k++;

                                $dar2[$key] = [
                                    'name' => $get_column['data'][$j]['attributes']['name'],
                                    'id' => $get_column['data'][$j]['id'],
                                ];
                                if ($parentId == array_search('dtjs',COLUMNS)) { //党团建设
                                    $dar1['id'] = array_search('dtjs',COLUMNS);
                                } else if ($parentId == array_search('ky',COLUMNS)) { //科研
                                    $dar1['id'] = array_search('ky',COLUMNS);
                                } else if ($parentId == array_search('jx',COLUMNS)) { //教学
                                    $dar1['id'] = array_search('jx',COLUMNS);
                                } else if ($parentId == array_search('rczp',COLUMNS)) {//人才
                                    $dar1['id'] = array_search('rczp',COLUMNS);
                                } else {
                                    $dar1['id'] = $dar2[1]['id'];
                                }
                            }
                        }
                    }
                } else {
                    $dar1 = [
                        'name' => $get_column['data'][$i]['attributes']['name'],
                        'id' => $menu_col
                    ];
                }
                $column = [
                    'name' => $get_column['data'][$i]['attributes']['name'],
                    'id' => $menu_col
                ];
            }
        }
        $this->assign("dar2", $dar2);
        $this->assign("web_title", $dar1['name']);
        $this->assign(["column" => $column,
            'data' => $data['data'],
            'bootstrap' => $bootstrap->render(),
            'dar1' => $dar1,
            'dar3' => $dar3
        ]);

        if (in_array($menu_col, PAGE_IMG)) {
            return $this->fetch("article_detail/page_more_img");
        }
        if ($menu_col == array_search('出诊信息',COLUMNS)) {//出诊信息
            $visit_info = $A->get_n_p_detail(PAGE['出诊信息']);
            $this->assign("visit_info", $visit_info['data']);
            return $this->fetch("article_detail/page_visit_info");
        }
        if ($menu_col == array_search('服务建议', COLUMNS)) {//服务建议
            return $this->fetch("article_detail/page_advise"/*,['builder'=> $builder]*/);
        }
        if ($menu_col == array_search('领导风采', COLUMNS)) {//领导风采
            return $this->fetch("article_detail/page_leader");
        }
        if ($menu_col == array_search('精彩视频', COLUMNS)) {
            return $this->fetch("article_detail/page_more_media");
        }
        if (in_array($menu_col, PAGE_DETAIL)|| $dar1['name'] == '就医指南') {
            $data = $A->get_sub_article($menu_col);
            $this->assign("data", $data['data']);
            return $this->fetch("article_detail/page_more_detail");
        } else {
            return $this->fetch("article_detail/page_more");
        }
    }

    public function doctor_detail()
    {
        $this->get_slider();//返回banner
        $A = new Api();
        $data = $A->get_menu('');
    }

}
